﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Interfaces.Filters;
using Interfaces.Repositories;
using Interfaces.RepositoryModels.InternalModels.NewsInformationModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SQLRepositories;
using SQLRepositories.NewsRepositories;

namespace DataProviderIntegrationTests.RepositoryTests
{
    [TestClass]
    public class FeedRepositoryTest
    {
        private readonly FeedRepository _repository = new FeedRepository(new DBEconomicInformationServiceContext());

        private FeedModel CreateFeed(Guid id, string url)
        {
            var feed = new FeedModel
                {
                    Id = id,
                    BaseUri = url,
                    Description = "Test",
                    Copyright = "Test",
                    Persons  = new Collection<PersonModel>
                        {
                            new PersonModel
                                {
                                    Email = "asasa@asd",
                                    Id = Guid.NewGuid(),
                                    Name = "name",
                                    Uri = "testuri",
                                    CreatedDate = DateTime.Now,
                                    ChangedDate = DateTime.Now
                                }
                        },
                    Language = "english",
                    LastUpdatedTime = DateTime.Now,
                    CreatedDate = DateTime.Now,
                    ChangedDate = DateTime.Now
                };
            _repository.Create(feed);
            return feed;
        }

        [TestMethod]
        public void FeedCreate_IntegrationTest()
        {
            var feedId = Guid.NewGuid();
            const string testUrl = "test";
            CreateFeed(feedId, testUrl);
            var result = _repository.GetById(feedId);
            Assert.IsNotNull(result);
        }

        [TestMethod]
        public void FeedGetData_IntegrationTest()
        {
            var feedId = Guid.NewGuid();
            const string testUrl = "test1";
            var itemId = Guid.NewGuid(); 
            
            var feed = CreateFeed(feedId, testUrl);

            feed.Items = new List<NewsDataItemModel>
                {
                    new NewsDataItemModel
                        {
                            BaseUri = "test",
                            Id = itemId,
                            LastUpdatedTime = DateTime.Now,
                            PublishDate = DateTime.Now,
                            Content = "asdsadsadasdasdasda",
                            CreatedDate = DateTime.Now,
                            ChangedDate = DateTime.Now
                            
                        }
                };
            _repository.Update(feed);

            var filter = new Mock<IFilter>();
            filter.Setup(x => x.Filtrate(It.IsAny<IEnumerable<object>>())).Returns((IEnumerable<object> y) => y);
            var ifeed = _repository.GetData(testUrl,filter.Object);
            Assert.IsNotNull(ifeed);
            Assert.IsNotNull(ifeed.Items);
            Assert.AreEqual(1,ifeed.Items.Count);
        }
    }
}
